def run_algorithm(outlets, user, green, power)
	   data = []
	   preference = user.last.consumption_preference
	   util_cntrl_lvl = user.last.utility_control_lvl
	   or_lvl = utility.last.override_lvl
	   #green power factor (GPF)
	   #GPF = (GreenSimulation.where(["logged_at like '%?%'",Time.now.hour]).last/PowerLog.last.power_consumption)
       green_percent = green / power
	
	   for outlet in outlets
	     if or_lvl <= util_cntrl_lvl and outlet.priority >= util_cntrl_lvl
	     	preference = 6
	     else
	     	preference = user.last.consumption_preference
	     end
		case preference
		when preference = 1
		 if outlet.priority != 1 and outlet.status == true
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 0)
		 elsif outlet.priority == 1 and outlet.status == false
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 1)
		 end
		when preference = 2
		  if green_percent >= 0.8 and outlet.priority < 3 and outlet.status == false
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 1)
		  elsif green_percent >= 1 and outlet.priority == 3 and outlet.status == false
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 1)
		  elsif green_percent < 0.8 and outlet.priority == 2 and outlet.status == true
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 0)
		  elsif green_percent < 1 and outlet.priority == 3 and outlet.status == true
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 0)
		  end
		when preference = 3
		  if green_percent >= 0.4 and outlet.priority < 3 and outlet.status == false
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 1)
		  elsif green_percent >= 0.6 and outlet.priority == 3 and outlet.status == false
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 1)
		  elsif green_percent < 0.4 and outlet.priority > 1 and outlet.status == true
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 0)
		  end
		when preference = 4
		  if green_percent >= 0.1 and outlet.priority < 3 and outlet.status == false
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 1)
		  elsif green_percent >= 0.3 and outlet.priority == 3 and outlet.status == false
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 1)
		  elsif green_percent < 0.1 and outlet.priority > 1 and outlet.status == true
			data << outlet.outlet_id
			o = OutletStatus.find(outlet.id)
			o.update_attribute(:status, 0)
		  end
		when preference = 5
			if outlet.status == false
			  data << outlet.outlet_id
			  o = OutletStatus.find(outlet.id)
			  o.update_attribute(:status, 1)
			end
		when preference = 6
			if outlet.status == true
			  data << outlet.outlet_id
			  o = OutletStatus.find(outlet.id)
			  o.update_attribute(:status, 0)
			end
		end
	   end
	    return data
	end
